#_*_coding:utf-8_*_
__author__ = 'kom'

from settings import *


class itemsSearchControler:
    def __init__(self):
        settings = __import__("settings")
        self.render = settings.render
    def GET(self):
        return "get"
    def POST(self):
        args = web.input()
        """ """
        tc = TopClient()
        tc.setKeys(appkey=web.config.appkey,secretKey=web.config.secretKey)
        tc.setGateWay(web.config.gatewayUrl)
        tc.setFormat(web.config.format)
        tc.setReq("ItemsSearchRequest")
        tc.setQuerys(args)
        res = tc.execute()
        """
        re = '{"items_search_response": {"total_results": 26, "item_search": {"items": {"item": [{"cid": 162404, "type": "fixed", "price": "118.00", "title": "2011\u65b0\u6b3e\u6625\u88c5\u676d\u6d3e\u5973\u88c5\u97e9\u7248\u6536\u8170\u663e\u7626\u6dd1\u5973\u5168\u68c9<span class=H>\u8fde\u8863\u88d9</span>\u683c\u5b50\u5927\u7801\u4e09\u4ef6\u5957", "num_iid": 6000154216, "volume": 10, "nick": "kukukafei06", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-07 00:13:48", "pic_url": "http://img08.tbsandbox.com/bao/uploaded/i8/T1nFdXXiW8B02ySvI._111930.jpg"}, {"cid": 50010850, "type": "fixed", "price": "78.37", "title": "\u97e9\u7248\u6dd1\u5973\u5706\u9886\u77ed\u8896<span class=H>\u8fde\u8863\u88d9</span> \u751c\u7f8e\u96ea\u7eba\u88d9", "num_iid": 1451973753, "volume": 20, "nick": "supplier1", "score": 1, "post_fee": "0.00", "delist_time": "2012-05-07 14:12:17", "pic_url": "http://img03.tbsandbox.com/bao/uploaded/i3/T1EpXXXnu7xtR6kxc0_035413.jpg"}, {"cid": 50011150, "type": "fixed", "price": "147.42", "title": "MumuHome. \u81f4\u547d\u8bf1\u60d1\u529b \u776b\u6bdb\u857e\u4e1d\u516c\u4e3b<span class=H>\u8fde\u8863\u88d9</span>1554", "num_iid": 1451973821, "volume": 30, "nick": "supplier1", "score": 1, "post_fee": "0.00", "delist_time": "2012-05-07 14:12:17", "pic_url": "http://img08.tbsandbox.com/bao/uploaded/i8/T1MFXXXlJDj0Ye3mEV_021646.jpg"}, {"cid": 50010850, "type": "fixed", "price": "90.00", "title": "\u97e9\u724809\u54c1\u724c\u5973\u88c5 \u6dd1\u5973\u53cc\u6392\u6263\u96ea\u7eba\u4e1d\u5dfe\u76f8\u62fc<span class=H>\u8fde\u8863\u88d9</span>\u5b50 ...\u3010\u6c99\u7bb1\u7248\u3011", "num_iid": 1451826415, "volume": 40, "nick": "taobao02", "score": 0, "post_fee": "8.00", "delist_time": "2012-05-07 19:46:53", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1zFXXXh.1.JOLzBYX_115951.jpg"}, {"cid": 50010850, "type": "fixed", "price": "90.00", "title": "\u97e9\u724809\u54c1\u724c\u5973\u88c5 \u6dd1\u5973\u53cc\u6392\u6263\u96ea\u7eba\u4e1d\u5dfe\u76f8\u62fc<span class=H>\u8fde\u8863\u88d9</span>\u5b50 ...\u3010\u6c99\u7bb1\u7248\u3011", "num_iid": 1451826413, "volume": 50, "nick": "taobao02", "score": 0, "post_fee": "8.00", "delist_time": "2012-05-07 19:46:53", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1zXXXXczc.JNx5RTX_115946.jpg"}, {"cid": 50010850, "type": "fixed", "price": "49.50", "title": "\u97e9\u724809\u54c1\u724c\u5973\u88c5 \u6dd1\u5973\u7f8e\u5973\u53cc\u5c42\u88d9\u5b50\u6446<span class=H>\u8fde\u8863\u88d9</span>\u5b50@DL98...\u3010\u6c99\u7bb1\u7248\u3011", "num_iid": 1451826409, "volume": 60, "nick": "taobao02", "score": 0, "post_fee": "8.00", "delist_time": "2012-05-07 19:46:53", "pic_url": "http://img07.tbsandbox.com/bao/uploaded/i7/T1zpXXXe2c.JPCJlHX_115930.jpg"}, {"cid": 50010850, "type": "fixed", "price": "49.00", "title": "\u97e9\u724809\u54c1\u724c\u5973\u88c5 \u6dd1\u5973\u70ed\u5356\u51b0\u4e1d\u68c9\u76f8\u62fc\u5bbd\u8170\u77ed\u8896<span class=H>\u8fde\u8863\u88d9</span>\u5b50 DL985928\u7c89", "num_iid": 1451823405, "volume": 0, "nick": "taobao02", "score": 0, "post_fee": "8.00", "delist_time": "2012-05-07 19:47:27", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1yFXXXc2Om0O0rCA1_042330.jpg"}, {"cid": 162103, "type": "fixed", "price": "98.00", "title": "\u767d\u9e1f\u82b1\u5b50 \u97e9\u7248 \u5806\u9886\u968f\u610f LOVE \u4eae\u7247 \u5154\u6bdb \u9ad8\u9886 \u6bdb\u8863 <span class=H>\u8fde\u8863\u88d9</span>138", "num_iid": 1451973979, "volume": 0, "nick": "axpwx", "score": 0, "post_fee": "10.00", "delist_time": "2012-05-07 19:47:30"}, {"cid": 50010850, "type": "fixed", "price": "50.00", "title": "<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 6000292247, "volume": 0, "nick": "ucipl0094", "score": 0, "post_fee": "10.00", "delist_time": "2012-05-09 08:35:14", "pic_url": "http://img03.tbsandbox.com/bao/uploaded/i3/T1nFdXXnP9B02TJ_s._083520.jpg"}, {"cid": 50010850, "type": "fixed", "price": "300.00", "title": "\u79cb\u6c34\u4f0a\u4eba\u9ed1\u8272<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 6000001431, "volume": 0, "nick": "hajj1987", "score": 0, "post_fee": "35.00", "delist_time": "2012-05-09 09:54:41", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1NFXXXl6loJWD6t30_034713.jpg"}, {"cid": 50010850, "type": "fixed", "price": "138.00", "title": "2011\u6625\u88c5\u65b0\u6b3e\u5973\u88c5\u70ed\u5356 \u65f6\u5c1a\u97e9\u7248\u4fee\u8eab\u6bdb\u5462<span class=H>\u8fde\u8863\u88d9</span> 9979", "num_iid": 6000315577, "volume": 0, "nick": "magie16036936", "score": 0, "post_fee": "12.00", "delist_time": "2012-05-09 18:28:08", "pic_url": "http://img06.tbsandbox.com/bao/uploaded/i6/T1oVdXXmRtMd1zq_g6_062808.jpg"}, {"cid": 1623, "type": "fixed", "price": "21.00", "title": "<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 6000319365, "volume": 0, "nick": "lg10086aa", "score": 0, "post_fee": "15.00", "delist_time": "2012-05-10 01:45:47", "pic_url": "http://img04.tbsandbox.com/bao/uploaded/i4/T1oFdXXhPgKt1.mvZU_014549.jpg"}, {"cid": 50011123, "type": "fixed", "price": "20.00", "title": "\u7ea2\u8272<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 1452069777, "volume": 0, "nick": "lurantest", "score": 0, "post_fee": "10.00", "delist_time": "2012-05-10 14:47:41", "pic_url": "http://img08.tbsandbox.com/bao/uploaded/i8/T1FVXXXlxKM0RrNMo4_052746.jpg"}, {"cid": 50000671, "type": "fixed", "price": "120.00", "title": "2010\u97e9\u7248\u79cb\u88c5\u65b0\u6b3e\u5973\u88c5\u5305\u81c0\u9ad8\u8170<span class=H>\u8fde\u8863\u88d9</span>\u5e7cu\u74f7 \u70ed\u5356\u9ad8\u54c1\u8d28\u6298\u4e24\u4ef6\u5305\u90ae", "num_iid": 6000198739, "volume": 0, "nick": "tyklhf", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-10 14:51:11", "pic_url": "http://img06.tbsandbox.com/bao/uploaded/i6/T1jFdXXaoz5dWMDEkW_025109.jpg"}, {"cid": 50000671, "type": "fixed", "price": "200.00", "title": "2010\u97e9\u7248\u79cb\u88c5\u65b0\u6b3e\u5973\u88c5\u5305\u81c0\u9ad8\u8170<span class=H>\u8fde\u8863\u88d9</span>22", "num_iid": 6000199669, "volume": 0, "nick": "tyklhf", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-10 14:55:28", "pic_url": "http://img03.tbsandbox.com/bao/uploaded/i3/T1jFdXXaAz5dVw0rEY_025527.jpg"}, {"cid": 50010850, "type": "fixed", "price": "99.00", "title": "09\u65b0\u8774\u8776\u9ad8\u8170\u751c\u7f8e\u65f6\u5c1a1902V\u9886\u96ea\u7eba\u857e\u4e1d\u788e\u82b1\u675f\u8170\u4e24\u4ef6\u5957<span class=H>\u8fde\u8863\u88d9</span>\u5a03\u5a03", "num_iid": 1451736905, "volume": 0, "nick": "ptest", "score": 0, "post_fee": "5.00", "delist_time": "2012-05-11 00:21:34", "pic_url": "http://img04.tbsandbox.com/bao/uploaded/i4/T1ypXXXaWam0O4ogHa_121226.jpg"}, {"cid": 50010850, "type": "fixed", "price": "168.00", "title": "\u6b63\u54c1&mdash;&mdash;\u4e13\u67dc\u65e5\u5355\u97e9\u7248\u857e\u4e1d\u788e\u82b1\u516c\u4e3b\u9ad8\u8170V\u9886\u7cd5\u4e24\u4ef6\u5957\u5a03\u5a03<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 1451736913, "volume": 0, "nick": "ptest", "score": 0, "post_fee": "5.00", "delist_time": "2012-05-11 00:28:27", "pic_url": "http://img08.tbsandbox.com/bao/uploaded/i8/T1yVXXXXSam0O8hGnb_122751.jpg"}, {"cid": 50010850, "type": "fixed", "price": "111.00", "title": "\u4e13\u67dc\u65e5\u5355\u6b63\u54c1\u97e9\u7248\u4e24\u4ef6\u5957\u857e\u4e1d\u788e\u82b1\u516c\u4e3b\u9ad8\u8170V\u9886\u7cd5\u5a03\u5a03<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 1451736915, "volume": 0, "nick": "ptest", "score": 0, "post_fee": "5.00", "delist_time": "2012-05-11 00:30:38", "pic_url": "http://img07.tbsandbox.com/bao/uploaded/i7/T1ypXXXbaam0QiUV2b_123018.jpg"}, {"cid": 50010850, "type": "fixed", "price": "238.00", "title": "\u725b\u4ed4<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 6000002279, "volume": 0, "nick": "hajj1987", "score": 0, "post_fee": "20.00", "delist_time": "2012-05-11 14:05:59"}, {"cid": 50010850, "type": "fixed", "price": "102.00", "title": "2011\u65b0\u6b3e\u6625\u88c5\u676d\u6d3e\u5973\u88c5\u6536\u8170\u663e\u7626\u65f6\u5c1a\u6dd1\u5973\u7eaf\u8272\u97e9\u7248\u957f\u8896<span class=H>\u8fde\u8863\u88d9</span>\u914d\u8170\u5e26", "num_iid": 6000156358, "volume": 0, "nick": "kukukafei06", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-13 04:13:48", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1npdXXd1pAJ1vAvE._111922.jpg"}, {"cid": 50010850, "type": "fixed", "price": "50.00", "title": "vivi\u63a8\u8350  Dazzlin  \u788e\u82b1 \u9ebb\u8d28<span class=H>\u8fde\u8863\u88d9</span> \u56db\u8272 \u767d\u8272 656035/P40", "num_iid": 6000269474, "volume": 0, "nick": "zwidsc", "score": 0, "post_fee": "6.00", "delist_time": "2012-05-13 13:28:03", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1updXXi7g4t20pSMT_012803.jpg"}, {"cid": 50010850, "type": "fixed", "price": "50.00", "title": "vivi\u63a8\u8350  Dazzlin  \u788e\u82b1 \u9ebb\u8d28<span class=H>\u8fde\u8863\u88d9</span> \u56db\u8272 \u767d\u8272 656035/P40", "num_iid": 6000270852, "volume": 0, "nick": "zwidsc", "score": 0, "post_fee": "6.00", "delist_time": "2012-05-13 13:51:19", "pic_url": "http://img03.tbsandbox.com/bao/uploaded/i3/T1uXdXXkPs4t2WGxZU_015119.jpg"}, {"cid": 50010850, "type": "fixed", "price": "50.00", "title": "vivi\u63a8\u8350Dazzlin\u788e\u82b1\u9ebb\u8d28<span class=H>\u8fde\u8863\u88d9</span>\u56db\u8272\u5165656035", "num_iid": 6000272730, "volume": 0, "nick": "zwidsc", "score": 0, "post_fee": "6.00", "delist_time": "2012-05-13 15:20:54", "pic_url": "http://img08.tbsandbox.com/bao/uploaded/i8/T1updXXnwi4t2i_M.Z_032455.jpg"}, {"cid": 162404, "type": "fixed", "price": "118.00", "title": "2011\u65b0\u6b3e\u6625\u88c5\u676d\u6d3e\u5973\u88c5\u97e9\u7248\u6536\u8170\u663e\u7626\u7eaf\u8272\u4e24\u4ef6\u5957\u4f11\u95f2\u6dd1\u5973<span class=H>\u8fde\u8863\u88d9</span>\u5b50", "num_iid": 6000153970, "volume": 0, "nick": "kukukafei06", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-13 19:13:48", "pic_url": "http://img04.tbsandbox.com/bao/uploaded/i4/T1npdXXeepAJ1X92I._111928.jpg"}, {"cid": 50010850, "type": "fixed", "price": "72.00", "title": "2011 \u6625\u590f\u65b0\u6b3e \u70ed\u5356\u5973\u88c5 \u7279\u4ef7 \u5305\u90ae ZARA\u98ce\u5c1a\u663e\u7626<span class=H>\u8fde\u8863\u88d9</span>[A7308]", "num_iid": 6000218674, "volume": 0, "nick": "yangjiwei", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-13 20:09:16", "pic_url": "http://img02.tbsandbox.com/bao/uploaded/i2/T1oVdXXdRUMd3TmX.._080921.jpg"}, {"cid": 50010850, "type": "fixed", "price": "60.00", "title": "<span class=H>\u8fde\u8863\u88d9</span>", "num_iid": 6000003868, "volume": 0, "nick": "lgztx", "score": 0, "post_fee": "0.00", "delist_time": "2012-05-16 23:27:12"}]}, "item_categories": {"item_category": [{"count": 18, "category_id": 50010850}, {"count": 1, "category_id": 162103}, {"count": 1, "category_id": 50011123}, {"count": 2, "category_id": 162404}, {"count": 1, "category_id": 1623}, {"count": 1, "category_id": 50011150}, {"count": 2, "category_id": 50000671}]}}}}'
        res = json.loads(re)
        """
        print res
        items = res["items_search_response"]["item_search"]["items"]["item"]
        avg_price = getAvgItems(items,"price")
        sum_volume = sumItems(items,"volume")
        chart_json = getChartJson(items,title="price Chart",xby="price",yby="volume")

        if res.has_key("msg"):
            return res["sub_msg"]
        else:
            #return res["items_search_response"]["item_search"]["items"]["item"]
            return self.render.index(items=res["items_search_response"],avgprice=avg_price,sumvolume=sum_volume,chartJson=chart_json)
